<template>
  <div class="login">
    <!-- navbar -->
    <van-nav-bar left-arrow @click-left="onClickLeft" />
    <div class="duanxin">短信登录</div>
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="mobile"
        label="手机号"
        name="mobile"
        left-icon="smile-o"
        placeholder="手机号"
        :rules="[
          { required: true, message: '请填写手机号', trigger: 'onBlur' },
          {
            pattern: /^1[3\5\7\8\9]\d{9}$/,
            message: '手机号格式不准确',
            trigger: 'onBlur'
          }
        ]"
      />
      <van-field
        v-model="code"
        center
        name="code"
        left-icon="smile-o"
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="[
          { required: true, message: '必填', trigger: 'onBlur' },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式不准确',
            trigger: 'onBlur'
          }
        ]"
      >
        <template #button>
          <!-- 倒计时 -->
          <van-count-down
            v-if="countDowmShow"
            :time="timer"
            format="ss 秒"
            @finish="countDownFinish"
          />
          <!-- 发送验证码按钮 -->
          <van-button
            v-else
            size="small"
            native-type="button"
            round
            type="info"
            @click="sendSms"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { sendSms, login } from '@/api/login'
export default {
  name: 'LoginIndex',
  data () {
    return {
      mobile: 13911111111, // 手机号
      code: 246810, // 验证码
      timer: 60 * 1000, // 倒计时时间
      countDowmShow: false
    }
  },
  methods: {
    onClickLeft () {
      this.$router.push('/')
    },

    // 登录
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner'
      })
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        this.$store.commit('login', res.data)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } catch (err) {
        this.$toast.fail('登录失败')
      }
    },

    // 发送验证码
    async sendSms () {
      await this.$refs.loginForm.validate('mobile') // 验证手机号能走到下面说明验证成功
      this.countDowmShow = true
      try {
        await sendSms(this.mobile)
      } catch (err) {
        this.$toast.fail(err.response.data.message)
      }
    },

    // 验证码倒计时结束
    countDownFinish () {
      this.countDowmShow = false
      this.timer = 2 * 1000
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100vh;
  background-color: #fff;
  .duanxin {
    margin-top: 40px;
    font-size: 20px;
    margin: 20px 20px;
  }
}
</style>
